#include <cstdio>
#include "Application.h"

static GlApplication * instance;

static void keyboard_handle(unsigned char key, int x, int y) {
    instance->on_key_down(key, false, x, y);
}

static void spec_keyboard_handle(int key, int x, int y) {
    instance->on_key_down(key, true, x, y);
}

static void draw_handle() {
    instance->draw();
}

static void timer_handle(int timer) {
    instance->on_timer(timer);
}


void GlApplication::init_timer(int timer_id, int interval) {
    glutTimerFunc(interval, timer_handle, timer_id);
}

static void _reshape(int width, int height){
    instance->on_reshape(width, height);
}

static void mouse(int button, int state, int x, int y) {
    instance->on_mouse(button, state, x, y);
}

static void motion(int x, int y) {
    instance->on_motion(x, y);
}

void GlApplication::attach() {
    instance = this;
    glutDisplayFunc(draw_handle);
    glutKeyboardFunc(keyboard_handle);
    glutSpecialFunc(spec_keyboard_handle);
    glutReshapeFunc(_reshape);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
}
